module executor.friend.client.ApplyContactExecutor;

import msgtrans;
import message.Constants;
import message.MsgDefine;
import google.protobuf;
import hunt.logging;
import client.ImClient;
import std.array;
import std.conv : to;

@TransportClient(msgClientName)
class ApplyContactExecutor : AbstractExecutor!(ApplyContactExecutor) {

  @MessageId(MESSAGE.CMD_APPLY_FRIEND_REQ)
  void applyFriend(TransportContext ctx, MessageBuffer buffer)
  {
    auto req = new ApplyContact();
    buffer.data.fromProtobuf!ApplyContact(req);
    TransportSession[] clients =  ImClient.getInstance().getImClient(to!ulong(req.toId));
    logInfo("clients  applyFriend ---- %s" ,req.toId);
    foreach (TransportSession client ; clients)
    {
      logInfo("CMD_APPLY_FRIEND_REQ ---- %s" ,req.toId);
      client.send(new MessageBuffer(MESSAGE.CMD_APPLY_FRIEND_REQ,req.toProtobuf.array));
    }
    //if (c !is null)
    //{
    //  c.send(new MessageBuffer(MESSAGE.CMD_APPLY_FRIEND_REQ,req.toProtobuf.array));
    //}else
    //{
    //  logWarning("sendMsg user %d is offline",req.toId);
    //}
  }

  @MessageId(MESSAGE.CMD_DEAL_FRIEND_RES)
  void dealApplyContact(TransportContext ctx, MessageBuffer buffer)
  {
    auto addFriend = new AddContact();
    buffer.data.fromProtobuf!AddContact( addFriend);
    TransportSession[] clients =  ImClient.getInstance().getImClient( to!ulong( addFriend.toId));
    foreach (TransportSession client ; clients)
    {
      client.send( new MessageBuffer( MESSAGE.CMD_DEAL_FRIEND_RES,addFriend.toProtobuf.array));
    }
    //if (c !is null)
    //{
    //  c.send( new MessageBuffer( MESSAGE.CMD_DEAL_FRIEND_RES,addFriend.toProtobuf.array));
    //}else
    //{
    //  logWarning( "sendMsg user %d is offline",addFriend.toId);
    //}
  }
}

